package com.tarena.lbs.business.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.param.UserLocationParam;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreChainQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreChainVO;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.servcie.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(tags = "店铺相关功能接口")
@RestController
public class StoreController {
    private static final Logger log = LoggerFactory.getLogger(StoreController.class);
    @Autowired
    private StoreService storeService;


    @ApiOperation(value = "查询店铺列表")
    @PostMapping("/admin/basic/store/getAll")
    public Result<PageResult<StoreVO>> pageList(@RequestBody StoreQuery query) throws BusinessException {
        log.info("查询用户列表参数{}",query);
        PageResult<StoreVO>  pageResult = storeService.storeList(query);
        return new Result<>(pageResult);
    }

    /**
     * 新增店铺
     * @param param
     * @return
     * @throws BusinessException
     */
    @ApiOperation(value = "新增店铺")
    @PostMapping("/admin/basic/store/add")
    public Result<Void> save(@RequestBody StoreParam param) throws BusinessException {
        log.info("新增店铺参数{}",param);
        storeService.save(param);
        return Result.success();
    }

    //查询 属于当前登录用户 所属商家 和满足区域条件的店铺列表
    @ApiOperation(value = "查询区域下的店铺列表")
    @PostMapping("/admin/basic/store/getStoreByCity")
    public Result<PageResult<StoreVO>> getStoreByCity(@RequestBody AreaStoreQuery query) throws BusinessException {
        log.info("查询区域下的店铺列表参数{}",query);
        PageResult<StoreVO> pageResult = storeService.getStoreByCity(query);
        return new Result<>(pageResult);
    }


    //手机小程序定位上报
    @ApiOperation(value = "手机小程序定位上报")
    @PostMapping("/basic/store/location")
    public Result<Void> location(@RequestBody UserLocationParam param) throws BusinessException{
        log.info("手机小程序定位上报参数{}",param);
        storeService.reported(param);
        return Result.success();
    }

    //删除店铺
    @ApiOperation(value = "删除店铺")
    @DeleteMapping("/admin/basic/store/delete/{id}")
    public Result<Void> delete(@PathVariable Integer id) throws BusinessException{
        log.info("删除店铺参数{}",id);
        storeService.delete(id);
        return Result.success();
    }

    @ApiOperation("账号查询回显数据")
    @GetMapping("/admin/basic/store/{id}")
    public Result<StoreVO> selectById(@PathVariable Integer id) throws BusinessException {
        log.info("查询店铺详情参数{}",id);
        StoreVO storeVO = storeService.selectById(id);
        return new Result<>(storeVO);
    }

    @ApiOperation("账号修改")
    @PutMapping("/admin/basic/store/update")
    public Result<Void> update(@RequestBody StoreParam param) throws BusinessException{
        log.info("更新商铺数据入参{}",param);
        storeService.update(param);
        return Result.success();
    }

    @ApiOperation("商铺启用")
    @PutMapping("/admin/basic/store/enable/{id}")
    public Result<Void> enable(@PathVariable Integer id) throws BusinessException{
        log.info("启动/禁用商铺数据入参{}",id);
        storeService.enable(id);
        return Result.success();
    }

    @ApiOperation("商铺禁用")
    @PutMapping("/admin/basic/store/disable/{id}")
    public Result<Void> disable(@PathVariable Integer id) throws BusinessException{
        log.info("启动/禁用商铺数据入参{}",id);
        storeService.disable(id);
        return Result.success();
    }

    @ApiOperation("店铺链接")
    @PostMapping("/admin/basic/chain/getAll")
    public Result<PageResult<StoreChainVO>> getChain(@RequestBody StoreChainQuery storeChainQuery) throws BusinessException{
        log.info("连锁店铺入参{}",storeChainQuery);
        PageResult<StoreChainVO> pageResult = storeService.getChain(storeChainQuery);
        return new Result<>(pageResult);
    }

    @ApiOperation("修改店铺链接状态为禁用")
    @PutMapping("/admin/basic/chain/disable/{id}")
    public Result<Void> disableChain(@PathVariable Integer id) throws BusinessException{
        log.info("修改店铺链接状态为禁用入参{}",id);
        storeService.disableChain(id);
        return Result.success();
    }


    @ApiOperation("修改店铺链接状态为启动")
    @PutMapping("/admin/basic/chain/enable/{id}")
    public Result<Void> enableChain(@PathVariable Integer id) throws BusinessException{
        log.info("修改店铺链接状态为启动入参{}",id);
        storeService.enableChain(id);
        return Result.success();
    }

}
